package edu.colorado.core;

import java.io.Serializable;
import java.util.TreeSet;

public class HighScoreList implements Serializable {
    private static final long serialVersionUID = 589293916106971545L;

    public static final int MAX_SCORES = 5;

    private TreeSet<HighScore> scores;

    public HighScoreList() {
        scores = new TreeSet<HighScore>();
    }

    public boolean addScore(HighScore score) {
        scores.add(score);
        
        while (scores.size() > MAX_SCORES) {
            scores.remove(scores.first());
        }
        
        return scores.contains(score);
    }
    
    public boolean isHighScore(int score) {
        if (scores.size() < MAX_SCORES) {
            return true;
        } else {
            return scores.first().getScore() < score;
        }
    }
    
    public HighScore[] getScores() {
        HighScore[] hs = new HighScore[scores.size()];
        
        int i = hs.length - 1;
        for (HighScore score : scores) {
            hs[i--] = score;
        }
        
        return hs;
    }
}
